package cn.ivicar.oss.resource;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.resource.BaseLoggerResource;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.oss.controller.ParamController;
import cn.ivicar.oss.entity.ResponseEntity;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("param")
@RequestScoped
public class ParamResource extends BaseLoggerResource {

    @Inject
    private ParamController paramController;

    @Path("pd")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getParamDefinition(@QueryParam("paramType") String paramType) throws CarsmartException {

        JSONArray jsonArray = paramController.getParamDefinitionList(paramType);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",jsonArray);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();

    }

    @Path("pd/update")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response updateParamDefinition(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.updateParamDefinition(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("pd")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addParamDefinition(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.addParamDefinition(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("pd")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteParamDefinition(@QueryParam("id") String id) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.deleteParamDefinition(id);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("ps")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getParamSetting() throws CarsmartException {

        JSONArray jsonArray = paramController.getParamSettingList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",jsonArray);
        return CacheUtil.noCache(Response.ok(jsonObject)).build();

    }

    @Path("ps/data")
    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response getParamSettingData() throws CarsmartException {

        ResponseEntity paramSettingsData = paramController.getParamSettingsData();
        return CacheUtil.noCache(Response.ok(paramSettingsData)).build();

    }

    @Path("ps/update")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response updateParamSettings(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.updateParamSettings(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("ps")
    @POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response addParamSettings(JSONObject jsonObject) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.addParamSettings(jsonObject);

        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }

    @Path("ps")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response deleteParamSettings(@QueryParam("id") String id) throws CarsmartException {

        ResponseEntity responseEntity = this.paramController.deleteParamSettings(id);
        return CacheUtil.noCache(Response.ok(responseEntity)).build();
    }
}
